package com.example.guiguiapplicatio.fragment.SigningArchives;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.fragment.home.PoorFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ServicebookFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String imageUrl;
    private String idCard;
    String signingId;
    ImageButton servic_back;
    ImageView imageView;
    public ServicebookFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PoorFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ServicebookFragment newInstance(String param1, String param2) {
        ServicebookFragment fragment = new ServicebookFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            imageUrl = getArguments().getString("imageUrl");
            idCard = getArguments().getString("idCard");
            signingId = getArguments().getString("signingId");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_total_detail_contract_type, container, false);
        imageView = root.findViewById(R.id.url_show);
        servic_back = root.findViewById(R.id.servic_back);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 创建 URL 对象
                    URL url = new URL(imageUrl);

                    // 打开 URL 连接
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                    // 设置请求方法
                    connection.setRequestMethod("GET");

                    // 连接
                    connection.connect();

                    // 获取输入流
                    InputStream inputStream = connection.getInputStream();

                    // 将输入流转换为 Bitmap
                    final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                    // 主线程更新 UI
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 在 ImageView 中显示图片
                            imageView.setImageBitmap(bitmap);
                        }
                    });

                    // 关闭输入流
                    inputStream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        servic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("idCard", idCard);
                bundle.putString("signingId", signingId);
                Navigation.findNavController(v).navigate(R.id.action_ServicebookFragment_to__SigningdetailFragment,bundle);
            }
        });
        return root;
    }

    @Override
    public void onResume() {
        BottomNavigationView bottomNavigationView=requireActivity().findViewById(R.id.nav_view);
        bottomNavigationView.setVisibility(View.INVISIBLE);
        //这两行代码设置隐藏底部导航栏，若要显示啧修改INVISIBLE为Visible（可见）
        super.onResume();
    }
}
